package com.ridu.project.beshid.controller;

import com.ridu.project.beshid.api.GeneralResponse;
import com.ridu.project.beshid.api.Status;
import com.ridu.project.beshid.api.User;
import com.ridu.project.beshid.entity.ExamSubject;
import com.ridu.project.beshid.repository.ExamSubjectRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * exam_subject
 * @author 大狼狗 2020-05-10
 */
@RestController
@RequestMapping("/examSubject")
public class ExamSubjectController {

    @Autowired
    private ExamSubjectRepository examSubjectRepository;

    /**
     * 新增或编辑
     */
    @PostMapping("/save")
    public Object save(ExamSubject examSubject){
        return examSubjectRepository.save(examSubject);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public GeneralResponse delete(Long id){
        GeneralResponse response = new GeneralResponse();
        ExamSubject examSubject=examSubjectRepository.findById(id).get();
        examSubjectRepository.delete(examSubject);
        response.setStatus(Status.SUCCESS);
        response.setMessage("删除成功");
        return response;
    }

    /**
     * 查询
     */
    @PostMapping("/find")
    public Object find(Long id){
        GeneralResponse response = new GeneralResponse();
        ExamSubject examSubject=examSubjectRepository.findById(id).get();
        response.setData(examSubject);
        response.setStatus(Status.SUCCESS);
        response.setMessage("成功");
        return response;
    }

    /**
     * 分页查询
     */
    @PostMapping("/list")
    public Object list(ExamSubject examSubject,
                       @RequestParam(required = false, defaultValue = "0") int currentPage,
                       @RequestParam(required = false, defaultValue = "10") int pageSize) {

        //创建匹配器，需要查询条件请修改此处代码
        ExampleMatcher matcher = ExampleMatcher.matchingAll();

        //创建实例
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(currentPage - 1, pageSize, sort);
        Page<ExamSubject> page = examSubjectRepository.findAll(new Specification<ExamSubject>() {
            @Override
            public Predicate toPredicate(Root<ExamSubject> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate isDelete = null;
                isDelete = cb.equal(root.<Boolean>get("deleted"), false);
                query.where(isDelete);
                return null;
            }
        }, pageable);
        GeneralResponse response = new GeneralResponse();
        response.setRows(page.getContent());
        response.setTotal(page.getTotalElements());
        return response;
    }

}